package exam;

import interceptor.InjectConnection;

import java.sql.Connection;
import java.util.List;

import utils.Var;
import utils.SessionVar;
import utils.DBConnection;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpSession;

import examinee.ExamineeInfo;
import examinee.Examinee;
import question.QuestionUtils;

/**
 * Created by IntelliJ IDEA.
 * User: Shawn
 * Date: 2009-6-28
 * Time: 21:52:54
 */
public class TestAction {

  private Connection con;

  @InjectConnection(autoCommit = DBConnection.AutoCommiteOff)
  public void setCon(Connection con) {
    this.con = con;
  }

  private ExamineeInfo einfo;
  private TestPaperInfo tinfo;
  private List<AnswerPaperInfo> alist;

  public String getTestPaper() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    long eid = (Long) session.getAttribute(SessionVar.EXAMINEE_ID);
    einfo = Examinee.getExaminee(con, eid);
    tinfo = TestPaper.getTestPaper(con, eid);

    if (tinfo.getState() == ExamVar.STATE_END) return "done";

    alist = AnswerPaper.getAnswerPaperList(con, tinfo.getTid());
    session.setAttribute(SessionVar.EXAM_QUESTION_LIST, alist);
    return Var.ACTION_SUCCESS;
  }

  public ExamineeInfo getEinfo() {
    return einfo;
  }

  public TestPaperInfo getTinfo() {
    return tinfo;
  }

  public List<AnswerPaperInfo> getAlist() {
    return alist;
  }

  private int index;
  private int sum;
  private AnswerPaperInfo ainfo;

  public String getQuestion() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    alist = (List<AnswerPaperInfo>) session.getAttribute(SessionVar.EXAM_QUESTION_LIST);
    ainfo = alist.get(index - 1);
    sum = alist.size();
    return Var.ACTION_SUCCESS;
  }

  public int getIndex() {
    return index;
  }

  public void setIndex(int index) {
    this.index = index;
  }

  public int getSum() {
    return sum;
  }

  public AnswerPaperInfo getAinfo() {
    return ainfo;
  }

  private int to;
  private String answer;

  public String fillAnswer() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    alist = (List<AnswerPaperInfo>) session.getAttribute(SessionVar.EXAM_QUESTION_LIST);
    AnswerPaperInfo info = alist.get(index - 1);
    info.setAnswer(answer);
    AnswerPaper.fillAnswer(con, info.getAid(), answer);

    to = to > alist.size() ? alist.size() : to;
    ainfo = alist.get(to - 1);
    index = to;
    sum = alist.size();
    return Var.ACTION_SUCCESS;
  }

  public void setTo(int to) {
    this.to = to;
  }

  public void setAnswer(String answer) {
    this.answer = answer;
  }

  public String keepAlive() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();
    long eid = (Long) session.getAttribute(SessionVar.EXAMINEE_ID);
    tinfo = TestPaper.getTestPaper(con, eid);
    long time = tinfo.getTime() + ExamVar.TEST_INTERVAL;
    TestPaper.setTime(con, tinfo.getTid(), time);
    return Var.ACTION_SUCCESS;
  }

  private int score;
  private int passScore;

  public String handInTest() throws Exception {
    HttpSession session = ServletActionContext.getRequest().getSession();

    alist = (List<AnswerPaperInfo>) session.getAttribute(SessionVar.EXAM_QUESTION_LIST);
    AnswerPaperInfo ainfo = alist.get(index - 1);
    ainfo.setAnswer(answer);
    AnswerPaper.fillAnswer(con, ainfo.getAid(), answer);

    long eid = (Long) session.getAttribute(SessionVar.EXAMINEE_ID);
    tinfo = TestPaper.getTestPaper(con, eid);
    passScore = tinfo.getPassScore();

    score = 0;
    for (AnswerPaperInfo info : alist) {
      if (info.getStandardAnswer().equals(info.getAnswer()))
        score += QuestionUtils.getValue(info.getTx(), tinfo);
    }

    TestPaper.setDone(con, tinfo.getTid(), score);
    Examinee.setScore(con, eid, score);

    String path = TestPaperUtils.generate(con, eid);
    TestPaperUtils.print(path);

    return Var.ACTION_SUCCESS;
  }

  public int getScore() {
    return score;
  }

  public int getPassScore() {
    return passScore;
  }
}
